R programming: packages
M1 MIDS/MFA/LOGOS |
| Année 2025 |
This lab is just an incentive to dig into R Packages (2e) by Wickham and Bryan
Introduction
Sharing/Storing/Organizing
Packages come with conventions
Packaging can be helpful to perform data analysis.
Setup
Automating package development is facilitated by a collection of packages.
devtools is the cornerstone of the collections
- with
rstudio(hands in hands) - with
vs code - with
positron
Package organization
Make sure you understand the different states of a package : source, bundled, binary, installed, in-memory.
- At first, we deal with a source package. Where is it hosted?
- How is the source organized?
- What are metadata for?
- What is the purpose of file
.Rbuildignore? - Why do we bundle packages?
Structure and states of a package
To initialize a package development directory.
Code
packpath <- "hmw2.ma7by020"
if (!fs::dir_exists(packpath)){
fs::dir_create(packpath)
}
usethis::create_package(packpath)Within directory hmw2.ma7by020, you should have the next organization
hmw2.ma7by020/
├── DESCRIPTION
├── .gitignore
├── hmw2.ma7by020.Rproj
├── NAMESPACE
├── R
└── .Rbuildignore
2 directories, 5 files
What is the difference between loading and attaching a package?
You will have to
- populate the
Rsubdirectory - update
DESCRIPTION
Reusing scripts
Styling
Testing
The workhorse of the development process is ::: {.cell}
Code
devtools::load_all():::
- Code and/or fix bugs
- Load the code
- Test the code
Checking Package state
Code
devtools::check()